<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>Create or update roles API | ElasticSearch 7.7 权威指南中文版</title>
	<meta name="keywords" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <meta name="description" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
	<script>
	var _link = 'security-api-put-role.html';
    </script>
</head>
<body>
<div class="main-container">
    <section id="content">
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/reference/7.7/security-api-put-role.html" rel="nofollow" target="_blank">https://www.elastic.co/guide/en/elasticsearch/reference/7.7/security-api-put-role.html</a>, 原文档版权归 www.elastic.co 所有<br/>本地英文版地址: <a href="../en/security-api-put-role.html" rel="nofollow" target="_blank">../en/security-api-put-role.html</a></div>
                        <!-- start body -->
                  <div class="page_header">
<strong>重要</strong>: 此版本不会发布额外的bug修复或文档更新。最新信息请参考 <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html" rel="nofollow">当前版本文档</a>。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch Guide [7.7]</a></span>
»
<span class="breadcrumb-link"><a href="rest-apis.html">REST APIs</a></span>
»
<span class="breadcrumb-link"><a href="security-api.html">Security APIs</a></span>
»
<span class="breadcrumb-node">Create or update roles API</span>
</div>
<div class="navheader">
<span class="prev">
<a href="security-api-put-role-mapping.html">« Create or update role mappings API</a>
</span>
<span class="next">
<a href="security-api-put-user.html">Create or update users API »</a>
</span>
</div>
<div class="section xpack">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="security-api-put-role"></a>Create or update roles API<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/x-pack/docs/en/rest-api/security/create-roles.asciidoc">edit</a><a class="xpack_tag" href="https://www.elastic.co/subscriptions"></a>
</h2>
</div></div></div>

<p>Adds and updates roles in the native realm.</p>
<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="security-api-put-role-request"></a>Request<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/x-pack/docs/en/rest-api/security/create-roles.asciidoc">edit</a>
</h3>
</div></div></div>
<p><code class="literal">POST /_security/role/&lt;name&gt;</code><br></p>
<p><code class="literal">PUT /_security/role/&lt;name&gt;</code></p>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="security-api-put-role-prereqs"></a>Prerequisites<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/x-pack/docs/en/rest-api/security/create-roles.asciidoc">edit</a>
</h3>
</div></div></div>
<div class="ulist itemizedlist">
<ul class="itemizedlist">
<li class="listitem">
To use this API, you must have at least the <code class="literal">manage_security</code> cluster
privilege.
</li>
</ul>
</div>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="security-api-put-role-desc"></a>Description<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/x-pack/docs/en/rest-api/security/create-roles.asciidoc">edit</a>
</h3>
</div></div></div>
<p>The role management APIs are generally the preferred way to manage roles, rather than using
<a class="xref" href="defining-roles.html#roles-management-file" title="File-based role management">file-based role management</a>. The create
or update roles API cannot update roles that are defined in roles files.</p>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="security-api-put-role-path-params"></a>Path parameters<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/x-pack/docs/en/rest-api/security/create-roles.asciidoc">edit</a>
</h3>
</div></div></div>
<div class="variablelist">
<dl class="variablelist">
<dt>
<span class="term">
<code class="literal">name</code>
</span>
</dt>
<dd>
(string) The name of the role.
</dd>
</dl>
</div>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="security-api-put-role-request-body"></a>Request body<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/x-pack/docs/en/rest-api/security/create-roles.asciidoc">edit</a>
</h3>
</div></div></div>
<p>The following parameters can be specified in the body of a PUT or POST request
and pertain to adding a role:</p>
<div class="variablelist">
<dl class="variablelist">
<dt>
<span class="term">
<code class="literal">applications</code>
</span>
</dt>
<dd>
<p>
(list) A list of application privilege entries.
</p>
<div class="variablelist">
<dl class="variablelist">
<dt>
<span class="term">
<code class="literal">application</code> (required)
</span>
</dt>
<dd>
(string) The name of the application to which this entry applies
</dd>
<dt>
<span class="term">
<code class="literal">privileges</code>
</span>
</dt>
<dd>
(list) A list of strings, where each element is the name of an application
privilege or action.
</dd>
<dt>
<span class="term">
<code class="literal">resources</code>
</span>
</dt>
<dd>
(list) A list resources to which the privileges are applied.
</dd>
</dl>
</div>
</dd>
<dt>
<span class="term">
<code class="literal">cluster</code>
</span>
</dt>
<dd>
(list) A list of cluster privileges. These privileges define the
cluster level actions that users with this role are able to execute.
</dd>
<dt>
<span class="term">
<code class="literal">global</code>
</span>
</dt>
<dd>
(object) An object defining global privileges. A global privilege is
a form of cluster privilege that is request-aware. Support for global privileges
is currently limited to the management of application privileges.
This field is optional.
</dd>
<dt>
<span class="term">
<code class="literal">indices</code>
</span>
</dt>
<dd>
<p>
(list) A list of indices permissions entries.
</p>
<div class="variablelist">
<dl class="variablelist">
<dt>
<span class="term">
<code class="literal">field_security</code>
</span>
</dt>
<dd>
(list) The document fields that the owners of the role have
read access to. For more information, see
<a class="xref" href="field-and-document-access-control.html" title="Setting up field and document level security">Setting up field and document level security</a>.
</dd>
<dt>
<span class="term">
<code class="literal">names</code> (required)
</span>
</dt>
<dd>
(list) A list of indices (or index name patterns) to which the
permissions in this entry apply.
</dd>
<dt>
<span class="term">
<code class="literal">privileges</code>(required)
</span>
</dt>
<dd>
(list) The index level privileges that the owners of the role
have on the specified indices.
</dd>
<dt>
<span class="term">
<code class="literal">query</code>
</span>
</dt>
<dd>
A search query that defines the documents the owners of the role have
read access to. A document within the specified indices must match this query in
order for it to be accessible by the owners of the role.
</dd>
</dl>
</div>
</dd>
<dt>
<span class="term">
<code class="literal">metadata</code>
</span>
</dt>
<dd>
(object) Optional meta-data. Within the <code class="literal">metadata</code> object, keys
that begin with <code class="literal">_</code> are reserved for system usage.
</dd>
<dt>
<span class="term">
<code class="literal">run_as</code>
</span>
</dt>
<dd>
(list) A list of users that the owners of this role can impersonate.
For more information, see
<a class="xref" href="run-as-privilege.html" title="Submitting requests on behalf of other users">Submitting requests on behalf of other users</a>.
</dd>
</dl>
</div>
<p>For more information, see <a class="xref" href="defining-roles.html" title="Defining roles">Defining roles</a>.</p>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="security-api-put-role-example"></a>Examples<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/x-pack/docs/en/rest-api/security/create-roles.asciidoc">edit</a>
</h3>
</div></div></div>
<p>The following example adds a role called <code class="literal">my_admin_role</code>:</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">POST /_security/role/my_admin_role
{
  "cluster": ["all"],
  "indices": [
    {
      "names": [ "index1", "index2" ],
      "privileges": ["all"],
      "field_security" : { // optional
        "grant" : [ "title", "body" ]
      },
      "query": "{\"match\": {\"title\": \"foo\"}}" // optional
    }
  ],
  "applications": [
    {
      "application": "myapp",
      "privileges": [ "admin", "read" ],
      "resources": [ "*" ]
    }
  ],
  "run_as": [ "other_user" ], // optional
  "metadata" : { // optional
    "version" : 1
  }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/2077.console"></div>
<p>A successful call returns a JSON structure that shows whether the role has been
created or updated.</p>
<div class="pre_wrapper lang-console-result">
<pre class="programlisting prettyprint lang-console-result">{
  "role": {
    "created": true <a id="CO656-1"></a><i class="conum" data-value="1"></i>
  }
}</pre>
</div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO656-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p>When an existing role is updated, <code class="literal">created</code> is set to false.</p>
</td>
</tr>
</table>
</div>
</div>

</div>
<div class="navfooter">
<span class="prev">
<a href="security-api-put-role-mapping.html">« Create or update role mappings API</a>
</span>
<span class="next">
<a href="security-api-put-user.html">Create or update users API »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>